function [ table ] = UpdateTable( table,v_sqr, y, weight,table_size )
% update table basing on the weighting average of new and old meaurements

%-------------------------TUNNING FACTORS----------------------------------
    Cd_apx=4; % approximated CdL
    discount_rate = 0.7; % discount factor
    discount_threshold = 1000; % discount old weight when exceed this limit 
%--------------------------------------------------------------------------    
    
    if (v_sqr >100) && (v_sqr <1024)
        
        % find the index to speed range
        i=ceil((v_sqr-100) /((1024-100)/table_size)); 
        
        % if the weighting is greater that a threshold value, 
        % discount the old weighting
        if table(i,2) > discount_threshold
            table(i,2) = table(i,2)*discount_rate;
        end
        y=y-Cd_apx*(v_sqr-((i-0.5)*((1024-100)/table_size)+100));
        table(i,1)= (table(i,1)*table(i,2)+y*weight)/(table(i,2)+weight);
        table(i,2)= table(i,2)+weight;
    end


end

